package com.nsgf.library.event;


import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

/**
 * @ClassName: RxBus
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2024/9/12 11:13
 * @UpdateUser: 更新者：
 * @UpdateDate: 2024/9/12 11:13
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class RxBus {
    private static volatile RxBus instance;
    private final Subject mBus;

    private RxBus() {
// toSerialized method made bus thread safe
        mBus = PublishSubject.create().toSerialized();
    }

    public static RxBus get() {
        return Holder.BUS;
    }

    public void post(Object obj) {
        mBus.onNext(obj);
    }

    public Observable toObservable(Class tClass) {
        return mBus.ofType(tClass);
    }

    public Observable toObservable() {
        return mBus;
    }

    public boolean hasObservers() {
        return mBus.hasObservers();
    }

    private static class Holder {
        private static final RxBus BUS = new RxBus();
    }
}
